<?php
if (!(isset($_SESSION))) {  
	session_start(); 
}
//required classes
if (isset($page)) {
	$basedir = realpath("..");
} else {
	$basedir = realpath(".");
}
require_once("{$basedir}/system/includes/php/class/dblayer.php");
require_once("{$basedir}/system/includes/php/class/dbloader.php");
require_once("{$basedir}/system/includes/php/class/mysqldb.php");
require_once("{$basedir}/system/includes/php/class/authloader.php");
require_once("{$basedir}/system/includes/php/class/auth.php");

//required includes
require_once("{$basedir}/system/includes/php/system.php");
require_once("{$basedir}/system/includes/php/router.php");

if (file_exists("{$basedir}/site/config/settings.php")) {
	//use a configuration loader to eval and return the appropriate configuration directives.
	//require_once("site/config/settings.php");
	$Q = parse_system_config("{$basedir}/site/config/settings.php");
	if ($Q) {
		if (file_exists("{$basedir}/site/config/db.php")) {
			
			$default = new dbloader;
			$dblayer = new dblayer;
			$default->loadDBconfigFile("{$basedir}/site/config/db.php",$dblayer);
			
			$Q{'USER_DB_NAME'} = $default->param("USER","dbname");
			$Q{'USER_DB_CONN'} = $default->param("USER","dbconn");
			$Q{'DB_NAME'} = $default->param("SYSTEM","dbname");
			$Q{'DB_CONN'} = $default->param("SYSTEM","dbconn");
			
			
			$Q{'head'}{'scripts'} = array();
			$Q{'head'}{'css'} = array();
			
			$providers = new authloader;
			if (file_exists("{$basedir}/site/config/providers.php")) {
				$providers->load_providers("{$basedir}/site/config/providers.php");
			} else {
				//die("The authentication providers file has not been created.");
			}
			
			$auth = new auth;
			$authenticated = $auth->check_access($default,$dblayer,$providers);
			
		}	else {
			die("The default database configuration file does not exist.");
		}
		
	} else {
		die("A configuration error has occurred.");
	}
} else {
	//die("You must create a settings file for this site.");
	if (!defined("INSTALL")) {
		header("Location: install.php");
	}
}

?>
